import json
import logging
from datetime import datetime
import requests

from app.db.database import SessionLocal
from app.db.crud import create_tpost
from app.db.models import TPost
from app.pacong.content_fetcher import ContentFetcher

logger = logging.getLogger(__name__)

class WeiboHotListFetcher(ContentFetcher):
    """微博热搜爬虫"""

    def __init__(self):
        super().__init__()
        self.source = "weibo"
        self.source_name = "微博热搜"
        self.content_type = 16  # 微博热搜的source_id设为1

    def fetch_content(self) -> list:
        """获取微博热搜数据"""
        url = "https://weibo.com/ajax/side/hotSearch"
        
        try:
            headers = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
                "Accept": "application/json, text/plain, */*",
                "Referer": "https://weibo.com/"
            }
            response = requests.get(url, headers=headers)
            response.raise_for_status()
            data = response.json()
            
            if data.get("ok") != 1:
                logger.error("微博热搜接口返回错误")
                return []
            
            # 处理响应数据
            hot_list = []
            for item in data["data"]["realtime"]:
                # 过滤广告
                if item.get("is_ad"):
                    continue
                    
                # 构建关键词
                keyword = item.get("word_scheme", f"#{item['word']}#")
                
                hot_item = {
                    "title": item["word"],
                    "url": f"https://s.weibo.com/weibo?q={keyword}",
                    "pub_date": datetime.now(),  # 添加发布时间
                    "content": "",  # 添加空内容
                    "short_content": "",  # 添加空的简短内容
                }
                hot_list.append(hot_item)
            
            return hot_list
            
        except Exception as e:
            logger.error(f"获取微博热搜数据失败: {str(e)}")
            return []


if __name__ == "__main__":
    # 测试代码
    fetcher = WeiboHotListFetcher()
    fetcher.fetch_and_save() 